package com.fxg.controller;


import com.alipay.api.AlipayApiException;
import com.fxg.bean.AlipayBean;
import com.fxg.service.PayService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 经过测试，notify_url 和 return_url 的区别：
 * notify_url: post请求，对付款成功或者失败的业务进行处理的，成功率高达99.99999%.所以业务放在这儿处理！
 * return_url: get方式的请求，主要是支付宝付款成功之后跳转的界面。这个url支付宝回应的成功率不是很高
 *             一旦用户界面关闭之后，该链接将请求不到！
 *             再加个注释
 */
@RestController()
@RequestMapping("order")
public class OrderController {
    @Resource
    private PayService payService;//调用支付服务

    /*阿里支付*/
    @PostMapping(value = "alipay")
    public String alipay(String out_trade_no,String subject,String total_amount,String body) throws AlipayApiException {

        return  payService.aliPay(new AlipayBean()
                .setBody(body)
                .setOut_trade_no(out_trade_no)
                .setTotal_amount(new StringBuffer().append(total_amount))
                .setSubject(subject));
    }

    /**
     * 支付宝异步回调
     */
    @PostMapping("/zhifubao_callback")
    public String zhifubaoCallback(HttpServletRequest request) {

        final String out_trade_no = request.getParameter("out_trade_no");
        final String subject = request.getParameter("subject");
        final String total_amount = request.getParameter("total_amount");
        final String body = request.getParameter("body");
        final String timeout_express = request.getParameter("timeout_express");
        final String product_code = request.getParameter("product_code");
        final String seller_id = request.getParameter("seller_id");


        System.out.println("=================开始遍历参数=================");

        System.out.println("out_trade_no = "+out_trade_no);
        System.out.println("subject = "+subject);
        System.out.println("total_amount = "+total_amount);
        System.out.println("body = "+body);
        System.out.println("timeout_express = "+timeout_express);
        System.out.println("product_code = "+product_code);
        System.out.println("seller_id = "+seller_id);

        System.out.println("==============================================");

        return "success";

    }

    /**
     * 支付宝同步回调，一旦页面关闭将请求不到！
     */
    @RequestMapping("success")
    public String success(HttpServletRequest request) {

        Map<String, String> parameterMap = new HashMap<String, String>();

        for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
            parameterMap.put(entry.getKey(), entry.getValue()[0]);
        }

        Set<String> set = parameterMap.keySet();
        System.out.println("=================开始同步回调接口=================");
        for (String s : set) {
            System.out.println(s+"="+parameterMap.get("s"));
        }
        return "hello";
    }


}
